package dao;

import java.sql.*;
import java.util.*;
import to.AlmacenProductoTO;

public class AlmacenProductoDAO extends GenericDAO{
    public AlmacenProductoDAO(Transaction transacction) {
	super(transacction);
    }

    @SuppressWarnings("unchecked")
    public List<AlmacenProductoTO> stockProducto(AlmacenProductoTO to) throws TransactionException {
        Connection conn;
        List<AlmacenProductoTO> list = new ArrayList();
        try {
            int i = 1;
            conn = (Connection) t.getConnection();
            String sqlCall = "{call uspVerificaStock(?,?)}";
            CallableStatement procedure = conn.prepareCall(sqlCall);
            procedure.setString(i++, to.getCAlmacen());
            procedure.setString(i++, to.getCProducto());
            ResultSet rs = procedure.executeQuery();

            while (rs.next()) {
                int j=0;
                AlmacenProductoTO vo = new AlmacenProductoTO(); 
                vo.setCProducto(rs.getString(++j));
                vo.setNStockActual(rs.getInt(++j));
                vo.setNStockMinimo(rs.getInt(++j));
                list.add(vo);
            }

            procedure.close();
            conn.close();

        } catch (SQLException e1) {
                throw new TransactionException(e1);
        }
        return list;
    }
}